package worldinruin.file.imp;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import worldinruin.core.IMap;
import worldinruin.core.imp.Map;

public class MapHandler {

	private static MapHandler instance;
	
	private HashMap<String, IMap> maps = new HashMap<String, IMap>();
	
	/**
	 * Loads maps.
	 */
	private MapHandler() {
		loadMaps();
	}
	
	/**
	 * Gets single instance of MapHandler.
	 * @return Single instance of MapHandler.
	 */
	public static MapHandler getInstance() {
		
		if (instance == null) {
			instance = new MapHandler();
		}
		
		return instance;
	}

	/**
	 * Saves a map.
	 * @param filename Name of the map.
	 * @param map Map object.
	 */
	public void saveMap(String filename, IMap map) {
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		
		try {
			fos = new FileOutputStream("src/worldinruin/resources/maps/" + filename);
			oos = new ObjectOutputStream(fos);
			
			oos.writeObject(map);
			
			oos.close();
			fos.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * Loads the map with the given name.
	 * @param filename Name of the map.
	 * @return Map with the given filename.
	 */
	public IMap loadMap(String filename) {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		IMap map = null;
		
			try {
				fis = new FileInputStream("src/worldinruin/resources/maps/" + filename);
				ois = new ObjectInputStream(fis);
				
				Object o = ois.readObject();
				
					map = (Map) o;
					
				ois.close();
				fis.close();
				
			} catch (FileNotFoundException e) {
				
				e.printStackTrace();
			} catch (IOException e) {
			
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
			
				e.printStackTrace();
			}
			return map;		
	}
	
	/**
	 * Loads all maps available.
	 */
	public void loadMaps() {
		java.io.File file = new java.io.File("src/worldinruin/resources/maps");
				
		for (String s : file.list()) {
			if (s.compareTo(".svn") != 0) {
				maps.put(s, (loadMap(s)));
			}
			
		}
	}
		
	/**
	 * Gets the map with given name.
	 * @param name Map name.
	 * @return Map object.
	 */
	public IMap getMap(String name) {
		return maps.get(name);
	}
	
	/**
	 * Gets an arraylist of maps.
	 * @return Arraylsit of maps.
	 */
	public ArrayList<IMap> getMaps() {
		/*ArrayList<IMap> tempList = new ArrayList<IMap>();
		for (String s : getMapNames()) {
			tempList.add(getMap())
			
		}*/
		return (ArrayList<IMap>) maps.values();
		
			
	}
	
	/**
	 * Gets mapnames.
	 * @return Set of strings.
	 */
	public Set<String> getMapNames() {
		return maps.keySet();
	}
	
}
